<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* @property CI_DB_active_record $db
* @property CI_DB_forge $dbforge
* @property CI_Config $config
* @property CI_Loader $load
* @property CI_Session $session
*/

    class Main_model extends CI_Model{
        
        public function _construct(){
            parent::_construct();
        }
        
        public function user_exist($email, $password){
            $query = "SELECT id FROM users WHERE email = '" . mysql_real_escape_string($email) . "' AND password = '" . mysql_real_escape_string($password) . "'";
            $result = $this->db->query($query) or die(mysql_erorr());
            if($result){
                if($result->num_rows() == 1)
                    return true; 
                else
                    return false;
            }
        }
        
        public function get_name($email){
            $query = "SELECT name FROM users WHERE email = '" . mysql_real_escape_string($email) . "'";
            $result = $this->db->query($query) or die(mysql_erorr());
            if($result){
                foreach ($result->result() as $row){
                    $name = $row->name;
                }
                return $name;
            }
            else
                return false;
        }
        
        public function register($name, $email, $password){
            $query = "INSERT INTO users VALUES (NULL, '" . mysql_real_escape_string($name) . "', '" . mysql_real_escape_string($email) . "', '" . mysql_real_escape_string($password) . "', '', '', 0)";
            $status = $this->db->query($query) or die(mysql_error());
            if($status)
                return true;
            else
                return false;
        }
        
        public function check_valid_user($email){
            $query = "SELECT id FROM users WHERE email = '" . mysql_real_escape_string($email) . "'";
            $result = $this->db->query($query) or die(mysql_error());
            if($result){
                if($result->num_rows() > 0)
                    return true;
                else
                    return false;
            }
        }
        
        public function get_user_id($email){
            $query = "SELECT id FROM users WHERE email = '" . mysql_real_escape_string($email) . "'";
            $result = $this->db->query($query);
            if($result){
                foreach($result->result() as $row){
                    $user_id = $row->id;
                }
                return $user_id;
            }
            else
                return false;
            
        }
        
        public function update_api_key($user_mail, $api_key){
            $query = "UPDATE users SET api_key = '" . mysql_real_escape_string($api_key) . "', try = try + 1 WHERE email = '" . mysql_real_escape_string($user_mail) . "' AND try = 0";
            $status = $this->db->query($query);
            if($status)
                return true;
            else
                return false;
        }
        
        public function get_user_api_key($email){
            $query = "SELECT api_key FROM users  WHERE email = '" . mysql_real_escape_string($email) . "'";
            $result = $this->db->query($query) or die(mysql_error());
            if($result){
                foreach($result->result() as $k => $row){
                    $api_key = $row->api_key;
                }
                return $api_key;
            }
            else return false;
        }
        
        public function chech_valid_api($api_key, $site){
            $query = "SELECT domains FROM users WHERE api_key='" . mysql_real_escape_string($api_key) . "' AND domains LIKE '%" . mysql_real_escape_string($site) . "%'";
            $result = $this->db->query($query) or die(mysql_error());
            if($result){
                foreach($result->result() as $k => $row){
                    $domains = $row->domains;
                    $note[] = $domains;
                }
                if(!empty($note))
                    return true;
                else 
                    return false;
            }
            else 
                return false;
        }
        
        public function get_subs_list(){
            $query = "SELECT subsc FROM subscribers";
            $result = $this->db->query($query) or die(mysql_error());
            if($result){
                foreach($result->result() as $row){
                    $subscs[] = $row->subsc;
                }
                return $subscs;
            }
            else 
                return false; 
        }
        
    }
    
?>
